package com.garlic.face.jlf.transaction;

import android.util.Log;

import com.garlic.face.jlf.ui.MainActivity;

/**
 * Created by Wells on 2018/8/17.
 */

public class ServiceInterface {
    private static final String TAG = ServiceInterface.class.getSimpleName();

    private ServiceInterface() {
    }

    ;

    private static class InterfaceHolder {
        private static final ServiceInterface mServiceInterface = new ServiceInterface();
    }

    public static ServiceInterface getInstance() {
        return InterfaceHolder.mServiceInterface;
    }

    /**
     * 获取商品信息
     *
     * @param barCode 条码
     * @param qty     数量
     */
    public void getGoodsInfo(final String barCode, final String qty) {
        ThreadPoolProxyFactory.getNormalThreadPoolProxy().execute(new Runnable() {
            @Override
            public void run() {
                String sendMsg = "";
                if (Integer.parseInt(qty) > 1) {
                    sendMsg = CarrfourUtil.getInstance().getGoodInfo(barCode, qty);
                } else {
                    sendMsg = CarrfourUtil.getInstance().getGoodInfo(barCode, "1");
                }
                SocketService soc = MainActivity.getSocketService();
                Log.d(TAG, "xxxxxx run: service: " + soc);
                if (soc != null) {
                    boolean b = soc.sendMsg(sendMsg);
                    Log.d(TAG, "xxxxxx run: send is ok : " + b);
                }
            }
        });
    }

    /**
     * @param supervisor 管理员
     * @param password   密码
     * @param barcode    商品条码
     * @param count      需要删除的数量
     */
    public void removeBarcode(final String supervisor, final String password, final String barcode, final String count) {
        ThreadPoolProxyFactory.getNormalThreadPoolProxy().execute(new Runnable() {
            @Override
            public void run() {
                String sendMsg = CarrfourUtil.getInstance().cancelBarcode(supervisor, password, barcode, count);
                SocketService soc = MainActivity.getSocketService();
                if (soc != null) {
                    boolean b = soc.sendMsg(sendMsg);
                    Log.d(TAG, "run: send is ok : " + b);
                }
            }
        });
    }

    /**
     * 获取会员卡信息
     *
     * @param vipNumber 手机号或卡号
     */
    public void getVipNumber(final String vipNumber) {
        ThreadPoolProxyFactory.getNormalThreadPoolProxy().execute(new Runnable() {
            @Override
            public void run() {
                String sendMsg = CarrfourUtil.getInstance().getVipInfo(vipNumber, "2");
                SocketService soc = MainActivity.getSocketService();
                if (soc != null) {
                    boolean b = soc.sendMsg(sendMsg);
                    Log.d(TAG, "run: send is ok : " + b);
                }
            }
        });
    }

    /**
     * 获取小计信息
     */
    public void getSubtotal() {
        ThreadPoolProxyFactory.getNormalThreadPoolProxy().execute(new Runnable() {
            @Override
            public void run() {
                String subtotalRequset = CarrfourUtil.getInstance().getSubtotal(String.valueOf(Means.getInstance().getRec_Tran().getCount()), Means.getInstance().getRec_Tran().getQty().toString());
                SocketService soc = MainActivity.getSocketService();
                if (soc != null) {
                    boolean b = soc.sendMsg(subtotalRequset);
                    Log.d(TAG, "run: send is ok : " + b);
                } else {
                    Log.d(TAG, "run: 111111");
                }
            }
        });
    }

    /**
     * 使用管理员密码取消本次交易
     *
     * @param supervisor
     * @param password
     */
    public void cancelTrade(final String supervisor, final String password) {
        ThreadPoolProxyFactory.getNormalThreadPoolProxy().execute(new Runnable() {
            @Override
            public void run() {
                String sendMsg = CarrfourUtil.getInstance().cancelTrade(supervisor, password);
                SocketService soc = MainActivity.getSocketService();
                if (soc != null) {
                    boolean b = soc.sendMsg(sendMsg);
                    Log.d(TAG, "run: send is ok : " + b);
                }
            }
        });
    }

    public void cashierLogin(String cashier, String cashierPwd) {
        final String loginRequset = CarrfourUtil.getInstance().getLoginInfo(cashier, cashierPwd);
        ThreadPoolProxyFactory.getNormalThreadPoolProxy().execute(new Runnable() {
            @Override
            public void run() {
                SocketService soc = MainActivity.getSocketService();
                if (soc != null) {
                    boolean b = soc.sendMsg(loginRequset);
                    Log.d(TAG, "run: send is ok : " + b);
                } else {
                    Log.d(TAG, "run: 111111");
                }
            }
        });
    }

    public void stateLogin() {
        final String stateRequest = CarrfourUtil.getInstance().getStateInfo();
        ThreadPoolProxyFactory.getNormalThreadPoolProxy().execute(new Runnable() {
            @Override
            public void run() {
                SocketService soc = MainActivity.getSocketService();
                if (soc != null) {
                    boolean b = soc.sendMsg(stateRequest);
                    Log.d(TAG, "run: send is ok : " + b);
                    System.out.println("run: send is ok : " + b);
                } else {
                    Log.d(TAG, "run: 111111");
                    System.out.println("run: 111111");
                }
            }
        });
    }

    public void logout(String cashier, String cashierPwd, String supervisor, String password) {
        final String logoutRequest = CarrfourUtil.getInstance().getLogoutInfo(cashier, cashierPwd, supervisor, password);
        ThreadPoolProxyFactory.getNormalThreadPoolProxy().execute(new Runnable() {
            @Override
            public void run() {
                SocketService soc = MainActivity.getSocketService();
                if (soc != null) {
                    boolean b = soc.sendMsg(logoutRequest);
                    Log.d(TAG, "run: send is ok : " + b);
                } else {
                    Log.d(TAG, "run: 111111");
                }
            }
        });
    }

    public void payTrade(String payno, String payid, String total, String paymm, String verifycode, String flag, String yftotal, String ftoken) {
        final String sendmsg = CarrfourUtil.getInstance().getPayInfo(payno, payid, total, paymm, verifycode, flag, yftotal, ftoken);
        ThreadPoolProxyFactory.getNormalThreadPoolProxy().execute(new Runnable() {
            @Override
            public void run() {
                SocketService soc = MainActivity.getSocketService();
                if (soc != null) {
                    boolean b = soc.sendMsg(sendmsg);
                    Log.d(TAG, "run: send is ok : " + b);
                } else {
                    Log.d(TAG, "run: 111111");
                }
            }
        });
    }

    public void superVisor(String superVisor, String superVisorPwd, String type){
        ThreadPoolProxyFactory.getNormalThreadPoolProxy().execute(new Runnable() {
            @Override
            public void run() {
                String sendMsg = CarrfourUtil.getInstance().supervisor(superVisor,superVisorPwd, type);
                SocketService soc = MainActivity.getSocketService();
                if (soc != null) {
                    boolean b = soc.sendMsg(sendMsg);
                    Log.d(TAG, "run: send is ok : " + b);
                } else {
                    Log.d(TAG, "run: 111111");
                }
            }
        });
    }

    public void releaseSocket(){
        ThreadPoolProxyFactory.getNormalThreadPoolProxy().execute(new Runnable() {
            @Override
            public void run() {
                Log.e("--------", "releaseSocket1");
                SocketService soc = MainActivity.getSocketService();
                if (soc != null) {
                    soc.releaseSocket();
                }
            }
        });
    }
}
